//package com.crawler.downloader;
//
//import com.crawler.api.CrawlerDownloader;
//import com.crawler.model.CrawlerPageInfo;
//import com.crawler.model.CrawlerRequestInfo;
//import lombok.extern.slf4j.Slf4j;
//import org.openqa.selenium.Cookie;
//import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.phantomjs.PhantomJSDriver;
//import org.openqa.selenium.phantomjs.PhantomJSDriverService;
//import org.openqa.selenium.remote.CapabilityType;
//import org.openqa.selenium.remote.DesiredCapabilities;
//import org.springframework.stereotype.Service;
//
//import java.util.Map;
//import java.util.concurrent.TimeUnit;
//
//@Service("seleniumPhantomjsDownloader")
//@Slf4j
//public class SeleniumPhantomjsDownloader implements CrawlerDownloader {
//
//    private String driverPath;
//
//    @Override
//    public CrawlerPageInfo download(CrawlerRequestInfo crawlerRequestInfo) {
//        // driver init
//        DesiredCapabilities dcaps = new DesiredCapabilities();
//        dcaps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, !crawlerRequestInfo.isValidateTLSCertificates());
//        dcaps.setCapability(CapabilityType.TAKES_SCREENSHOT, false);
//        dcaps.setCapability(CapabilityType.SUPPORTS_FINDING_BY_CSS, true);
//        dcaps.setJavascriptEnabled(true);
//        if (driverPath!=null && driverPath.trim().length()>0) {
//            dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, driverPath);
//        }
//
//        if (crawlerRequestInfo.getProxy() != null) {
//            dcaps.setCapability(CapabilityType.ForSeleniumServer.AVOIDING_PROXY, true);
//            dcaps.setCapability(CapabilityType.ForSeleniumServer.ONLY_PROXYING_SELENIUM_TRAFFIC, true);
//            System.setProperty("http.nonProxyHosts", "localhost");
//            dcaps.setCapability(CapabilityType.PROXY, crawlerRequestInfo.getProxy());
//        }
//
//        /*dcaps.setBrowserName(BrowserType.CHROME);
//        dcaps.setVersion("70");
//        dcaps.setPlatform(Platform.WIN10);*/
//
//        WebDriver webDriver = new PhantomJSDriver(dcaps);
//
//        try {
//            // driver run
//            webDriver.get(crawlerRequestInfo.getUrl());
//
//            if (crawlerRequestInfo.getCookieMap() != null && !crawlerRequestInfo.getCookieMap().isEmpty()) {
//                for (Map.Entry<String, String> item: crawlerRequestInfo.getCookieMap().entrySet()) {
//                    webDriver.manage().addCookie(new Cookie(item.getKey(), item.getValue()));
//                }
//            }
//
//            webDriver.manage().timeouts().implicitlyWait(crawlerRequestInfo.getTimeoutMillis(), TimeUnit.MILLISECONDS);
//            webDriver.manage().timeouts().pageLoadTimeout(crawlerRequestInfo.getTimeoutMillis(), TimeUnit.MILLISECONDS);
//            webDriver.manage().timeouts().setScriptTimeout(crawlerRequestInfo.getTimeoutMillis(), TimeUnit.MILLISECONDS);
//
//            String pageSource = webDriver.getPageSource();
//            CrawlerPageInfo crawlerPageInfo=new CrawlerPageInfo();
//            crawlerPageInfo.setBody(pageSource);
//            return crawlerPageInfo;
//        } catch (Exception e) {
//            log.error(e.getMessage(), e);
//        } finally {
//            if (webDriver != null) {
//                webDriver.quit();
//            }
//        }
//        return null;
//    }
//}
